package com.ja.openchat.adapters;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.module.BaseLoadMoreModule;
import com.chad.library.adapter.base.module.LoadMoreModule;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.ja.openchat.R;
import com.zy.openchat.core.model.call.RemoteCallInfo;
import com.zy.openchat.core.model.user.UserInfo;
import com.zy.openchat.util.TimeConvertUtils;
import com.zy.openchat.util.TimeUtils;

import org.jetbrains.annotations.NotNull;

public class LatestCallRecordAdapter extends BaseQuickAdapter<RemoteCallInfo, BaseViewHolder> implements LoadMoreModule {

    private int recordType = 0;
    private long currUserId = 0l;

    public LatestCallRecordAdapter(int recordType) {
        super(R.layout.item_lastest_call_record);
        this.recordType = recordType;
        currUserId = UserInfo.getUserInfo().id;

    }


    @Override
    protected void convert(@NotNull BaseViewHolder helper, RemoteCallInfo model) {
        // 判断是呼出还是呼入
        if (currUserId == model.from) {// 自己发起的
            // 邀请对象用户的id
            long toUserId = model.to.get(0);
            // 会话被邀请者
            String toName = UserInfo.userDisplayName(toUserId);
            helper.setText(R.id.trade_title, toName);
            // 状态设置
            helper.setText(R.id.trade_date, model.isVideo ? getContext().getString(R.string.call_video_text) : getContext().getString(R.string.dial_voice_text));
        } else {// 被邀请
            // 会话发起者
            String formName = UserInfo.userDisplayName(model.from);
            helper.setText(R.id.trade_title, formName);
            // 状态设置
            helper.setText(R.id.trade_date, model.isVideo ? getContext().getString(R.string.call_video_text) : getContext().getString(R.string.dial_voice_text));
        }
        // icon设置
        if (model.isVideo) {
            helper.setImageResource(R.id.icon_call, R.mipmap.chat_icon_shipin);
        } else {
            helper.setImageResource(R.id.icon_call, R.mipmap.chat_icon_guaduan);
        }
        helper.setText(R.id.trade_value, TimeUtils.getMsgFormatTimeYYMMDDHHMM(model.createAt * 1000));
        if (0 == model.enterAt || 0 == model.leaveAt) {
            helper.setText(R.id.trade_status, getContext().getString(R.string.missed_call_text));
        } else {// 通话时长计算: 挂断时间-进入时间
            long durationTime = model.leaveAt - model.enterAt;
            helper.setText(R.id.trade_status, String.format(getContext().getString(R.string.duration_format_text), TimeConvertUtils.formatLongTime(durationTime * 1000)));
        }
    }

    @Override
    public @NotNull BaseLoadMoreModule addLoadMoreModule(@NotNull BaseQuickAdapter<?, ?> baseQuickAdapter) {
        return new BaseLoadMoreModule(baseQuickAdapter);
    }
}
